package edu.psu.collegerecommendationhelper;

import java.io.File;
import java.io.IOException;

import model.Customer;

import weka.classifiers.Evaluation;
import weka.classifiers.functions.LinearRegression;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NominalToString;
import weka.filters.unsupervised.attribute.StringToWordVector;
import weka.core.converters.ConverterUtils.DataSource;

public class WekaToolUtility implements Runnable{

	public void buildNaiveBayesModel() {
/*
		
	    // load CSV
	    CSVLoader loader = new CSVLoader();
	    try {
			loader.setSource(new File("/Users/cbarone/Desktop/test1.csv"));
		} catch (IOException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
	    Instances data = null;
		try {
			data = loader.getDataSet();
		} catch (IOException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
	 
	    // save ARFF
	    ArffSaver saver = new ArffSaver();
	    saver.setInstances(data);
	    try {
			saver.setFile(new File("/Users/cbarone/Desktop/test1.arff"));
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
	    try {
			saver.setDestination(new File("/Users/cbarone/Desktop/test1.arff"));
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
	    try {
			saver.writeBatch();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
	*/	
	    DataSource source = null;
		try {
			source = new DataSource("/Users/cbarone/Desktop/test1.arff");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Instances raw = null;
		try {
			raw = source.getDataSet();
			//raw = source.getStructure();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		NominalToString nfilter = new NominalToString();
	    try {
	    	nfilter.setInputFormat(raw);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	    
	    Instances dataFiltered = null;
		try {
			dataFiltered = Filter.useFilter(raw, nfilter);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		StringToWordVector sfilter = new StringToWordVector();
	    try {
	    	sfilter.setInputFormat(raw);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			dataFiltered = Filter.useFilter(raw, sfilter);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		if (dataFiltered.classIndex() == -1)
			dataFiltered.setClassIndex(dataFiltered.numAttributes() - 1);
		
		    // train NaiveBayes
		LinearRegression lr = new LinearRegression();
	    try {
			lr.buildClassifier(dataFiltered);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	    /*
	    Instance curr;
	    try {
			while ((curr = loader.getNextInstance(dataFiltered)) != null)
				try {
					lr.classifyInstance(curr);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   */	
	   
	    // output generated model
	    System.out.println(lr);
	    
		//loader.setFile(new File("/Users/cbarone/Desktop/test.csv"));
		//Instances test = loader.getStructure();

	//lr.classifyInstance(test);


		 
	    //return nb;
	}
	
	public void evaluateTestData(LinearRegression lr, Customer c) {


	}

	public static void main(String[] args) {

	       (new Thread(new WekaToolUtility() )).start();
		}

		@Override
		public void run() {

				buildNaiveBayesModel();

			
		}
}
